package com.fay.learn.config;

import com.fay.learn.ConfigProperties;
import com.fay.learn.server.TestConfigServe;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author fay
 * @date 2021-01-29
 */
@Configuration
@ConditionalOnClass(TestConfig.class)
@EnableConfigurationProperties(ConfigProperties.class)
@ConditionalOnProperty(prefix = "learn", name = "enable", havingValue = "true", matchIfMissing = true)
public class TestConfigConfiguration {
    @Resource
    private ConfigProperties configProperties;

    @Bean
    @ConditionalOnMissingBean(TestConfig.class)
    public TestConfig config() {
        TestConfig testConfig = new TestConfig();
        testConfig.setId(configProperties.getId());
        testConfig.setName(configProperties.getName());
        return testConfig;
    }

    @Bean
    @ConditionalOnMissingBean(TestConfigServe.class)
    public TestConfigServe serve() {
        return new TestConfigServe(this.config());
    }
}
